﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Threading;
using System.Collections;

namespace EmaillSend
{
    public class MailParam
    {
        private MailServer mailserver;

        public MailServer Mailserver
        {
            get { return mailserver; }
            set { mailserver = value; }
        }
        private MailContent mailcontent;

        public MailContent Mailcontent
        {
            get { return mailcontent; }
            set { mailcontent = value; }
        }
        private string mailTo;

        public string MailTo
        {
            get { return mailTo; }
            set { mailTo = value; }
        }

        private string id;

        public string Id
        {
          get { return id; }
          set { id = value; }
        }

        public MailParam(MailServer server,MailContent content,string to,string guid)
        {
            this.mailserver = server;
            this.mailcontent = content;
            this.mailTo = to;
            this.id = guid;
        }

    }

    public class MailThread
    {
        public ManualResetEvent eventX;
        public static IList guidlist = new ArrayList(); 
        public void sendMail(Object obj)
        {
            MailServer server = (MailServer)obj;
            if (server.sendList.Count > 0)
            {
                foreach (SendParam param in server.sendList)
                {
                    try
                    {
                        MailMessage message = param.Mailcontent.buildMessage(param.MailTo.Trim());
                        message.From = new MailAddress(server.mailaddress.Trim(),server.showname.Trim());
                        message.To.Add(new MailAddress(param.MailTo.Trim()));
                        message.ReplyToList.Add(new MailAddress(server.backaddress.Trim()));
                        //SmtpClient client = param.Mailserver.getClient();
                        //LogHelper.InfoLog(typeof(MailThread), "线程开始通过邮箱：" + param.Mailserver.mailaddress.Trim() + ",端口：" + param.Mailserver.port + "发送邮件给" + param.MailTo.Trim());
                        //判断当前是否正在发送                
                        //client.Send(message);
                        //client.Dispose();
                        server.SendMailMessage(message);
                        LogHelper.InfoLog("From：" + server.mailaddress.Trim() + ",端口：" + server.port + ",TO:" + param.MailTo.Trim() + "发送完成");
                        //更新数据库
                        lock (guidlist)
                        {
                            guidlist.Add(param.Id);
                        }                        
                    }
                    catch (Exception e)
                    {
                        LogHelper.ErrorLog("From:" + server.mailaddress + ",端口:" + server.port + ",TO:" + param.MailTo + ",Message:" + e.Message);
                    }
                    Thread.Sleep(Convert.ToInt32(Properties.Settings.Default.SendMailTime)*1000);
                }
            }            
        }
    }
}
